﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions events
        //////////////////////////FONCTIONS EVENTS///////////////////////////////////////

        //Ajout d'un event
        private bool add_events()
        {
            events event1 = new events();
            event_management event_manager1 = new event_management();

            if (event_name_textbox.Text != "")
            {
                event1.name = event_name_textbox.Text.ToLower();
            }

            //Attribution du créateur de l'événement
            if (userid != 0 && usertype != "")
            {
                event1.creator_id = userid;
                event1.creator_type = usertype;
            }
            else { event1.creator_id = 0; event1.creator_type = ""; }

            //Attribution du campus et de la classe
            if (event_campus_combobox.Text != "")
            {
                event1.campus = event_campus_combobox.Text.ToLower();
            }
            else { event1.campus = ""; }

            if (event_class_combobox.Text != "")
            {
                event1.classe = event_class_combobox.Text.ToLower();
            }
            else { event1.classe = ""; }

            //Ajout de la salle et attribution du statut "occupé" (available = 0)
            if (event_room_combobox.Text != "")
            {
                int room_id_temp = (from i in db_remote.rooms
                                    where i.name.Equals(event_room_combobox.SelectedValue)
                                    select i.id).FirstOrDefault();

                event1.room_id = room_id_temp;

                rooms room_temp = (from e in db_remote.rooms
                                   where e.id == room_id_temp
                                   select e).FirstOrDefault();

                room_temp.available = 0;
            }
            else { event1.room_id = 0; }

            //Type du participant
            if (event_participanttype_combobox.Text != "")
            {
                event1.type_person = event_participanttype_combobox.Text;
            }
            else { event1.type_person = ""; }

            //Nom du participant (id)
            if (event_participanttype_combobox.Text != "" && event_participanttype_combobox.Text != "")
            {

                string participantName = event_participantname_combobox.SelectedValue.ToString().ToLower();
                string participantType = event_participanttype_combobox.SelectedValue.ToString();

                event1.id_person = event_manager1.give_me_id_person_from_participant_and_type_person(participantType, participantName);

            }
            else { event1.id_person = 0; }

            //Type de l'événement
            if (event_type_combobox.Text != "")
            {
                event1.type = event_type_combobox.Text;

                if (usertype == "students" || usertype == "teachers")
                {
                    if (event_type_combobox.Text != "Personal")
                    {
                        return false;
                    }
                }
            }
            else { event1.type = ""; }

            //Priorité de l'événement
            if (event_priority_combobox.Text == "Optional")
            {
                event1.priority = 0;
            }
            else
            {
                event1.priority = 1;
            }


            //Ajout de la période
            if (event_period_combobox.Text != "")
            {

                string current_campus = event_campus_combobox.Text;
                string current_class = event_class_combobox.Text;

                int current_cursus_id = 0;

                current_cursus_id = (from e in db_remote.classes
                                     where e.name == current_class
                                     select (int)e.cursus_id).FirstOrDefault();

                int period_id_temp = (from i in db_remote.periods
                                      where i.name_period.Equals(current_cursus_id + "?" + event_period_combobox.SelectedValue)
                                      select i.id).FirstOrDefault();

                event1.period_id = period_id_temp;
            }
            else { event1.period_id = 0; }

            //Ajout de la matière
            if (event_subject_combobox.Text != "")
            {

                string current_period = event_period_combobox.Text;
                string current_class = event_class_combobox.Text;

                int current_cursus_id = 0;

                current_cursus_id = (from e in db_remote.classes
                                     where e.name == current_class
                                     select (int)e.cursus_id).FirstOrDefault();

                int subject_id_temp = (from i in db_remote.subjects
                                       where i.name.Equals(event_subject_combobox.SelectedValue + "_s" + current_cursus_id)
                                      select i.id).FirstOrDefault();

                event1.subject_id = subject_id_temp;
            }
            else { event1.subject_id = 0; }


            //Heure de début et de fin de l'événement
            if (event_startingdate_combobox.Text != "" && event_endingdate_combobox.Text != "")
            {
                string startingdate;
                string endingdate;

                startingdate = event_startingdate_datepicker.Text + " " + event_startingdate_combobox.Text;
                endingdate = event_endingdate_datepicker.Text + " " + event_endingdate_combobox.Text;

                event1.T_begin = Convert.ToDateTime(startingdate);
                event1.T_end = Convert.ToDateTime(endingdate);
            }

            db_remote.events.InsertOnSubmit(event1);
            db_remote.SubmitChanges();

            //Rafraichissement des éléments
            refresh_events();

            return true;

        }

        //Mise à jour d'un event
        private void update_events()
        {

            events event2 = (from e in db_remote.events
                             where e.name == event_listbox.SelectedValue.ToString()
                             select e).FirstOrDefault();

            event_management event_manager2 = new event_management();

            //Ajout du nom
            if (event_name_textbox.Text != "")
            {
                event2.name = event_name_textbox.Text.ToLower();
            }

            //Attribution du créateur de l'événement
            if (userid != 0 && usertype != "")
            {
                event2.creator_id = userid;
                event2.creator_type = usertype;
            }
            else { event2.creator_id = 0; event2.creator_type = ""; }

            //Attribution du campus et de la classe
            if (event_campus_combobox.Text != "")
            {
                event2.campus = event_campus_combobox.Text.ToLower();
            }
            else { event2.campus = ""; }

            if (event_class_combobox.Text != "")
            {
                event2.classe = event_class_combobox.Text.ToLower();
            }
            else { event2.classe = ""; }

            //Ajout de la salle
            if (event_room_combobox.Text != "")
            {

                int room_id_temp = (from i in db_remote.rooms
                                    where i.name.Equals(event_room_combobox.SelectedValue)
                                    select i.id).FirstOrDefault();

                event2.room_id = room_id_temp;

                rooms room_temp2 = (from e in db_remote.rooms
                                    where e.id == room_id_temp
                                    select e).FirstOrDefault();

                room_temp2.available = 0;
            }
            else { event2.room_id = 0; }


            //Ajout de la période
            if (event_period_combobox.Text != "")
            {
                string current_campus = event_campus_combobox.Text;
                string current_class = event_class_combobox.Text;

                int current_cursus_id = 0;

                current_cursus_id = (from e in db_remote.classes
                                     where e.name == current_class
                                     select (int)e.cursus_id).FirstOrDefault();

                int period_id_temp = (from i in db_remote.periods
                                      where i.name_period.Equals(current_cursus_id + "?" + event_period_combobox.SelectedValue.ToString())
                                      select i.id).FirstOrDefault();

                event2.period_id = period_id_temp;
            }
            else { event2.period_id = 0; }

            //Ajout de la matière
            if (event_subject_combobox.Text != "")
            {

                string current_period = event_period_combobox.Text;
                string current_class = event_class_combobox.Text;

                int current_cursus_id = 0;

                current_cursus_id = (from e in db_remote.classes
                                     where e.name == current_class
                                     select (int)e.cursus_id).FirstOrDefault();

                int subject_id_temp = (from i in db_remote.subjects
                                       where i.name.Equals(event_subject_combobox.SelectedValue + "_s" + current_cursus_id)
                                       select i.id).FirstOrDefault();

                event2.subject_id = subject_id_temp;
            }
            else { event2.subject_id = 0; }


            //Type du participant
            if (event_participanttype_combobox.Text != "")
            {
                event2.type_person = event_participanttype_combobox.Text;
            }
            else { event2.type_person = ""; }

            //Nom du participant (id)
            if (event_participantname_combobox.Text != "" && event_participanttype_combobox.Text != "")
            {
                string participantName = event_participantname_combobox.Text.ToLower();
                string participantType = event_participanttype_combobox.Text;

                event2.id_person = event_manager2.give_me_id_person_from_participant_and_type_person(participantType, participantName);

            }
            else { event2.id_person = 0; }

            //Type de l'événement
            if (event_type_combobox.Text != "")
            {
                event2.type = event_type_combobox.Text;
            }
            else { event2.type = ""; }

            //Priorité de l'événement
            if (event_priority_combobox.Text == "Optional")
            {
                event2.priority = 0;
            }
            else
            {
                event2.priority = 1;
            }


            //Heure de début et de fin de l'événement
            if (event_startingdate_combobox.Text != "" && event_endingdate_combobox.Text != "")
            {
                string startingdate;
                string endingdate;

                startingdate = event_startingdate_datepicker.Text + " " + event_startingdate_combobox.Text;
                endingdate = event_endingdate_datepicker.Text + " " + event_endingdate_combobox.Text;

                event2.T_begin = Convert.ToDateTime(startingdate);
                event2.T_end = Convert.ToDateTime(endingdate);
            }

            db_remote.SubmitChanges();

            //Rafraichissement des éléments
            refresh_events();

        }


        //Suppression d'un event
        private void delete_events()
        {
            if (event_name_textbox.Text != "")
            {

                events event3 = (from e in db_remote.events
                                 where e.name == event_listbox.SelectedValue.ToString()
                                 select e).FirstOrDefault();

                //On libère la salle

                if (event3.room_id != 0)
                {
                    rooms room_temp3 = (from e in db_remote.rooms
                                        where e.id == event3.room_id
                                        select e).FirstOrDefault();

                    room_temp3.available = 1;
                }


                db_remote.events.DeleteOnSubmit(event3);
                db_remote.SubmitChanges();

                //Rafraichissement des éléments
                refresh_events();
            }

        }

        //Listing des events enregistrer
        private void list_events(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (events s in (from e in db_remote.events select e))
            {
                maListe.Items.Add(s.name);
            }
        }


        //Affichage de la liste des events
        private void display_events(String events)
        {
            //On nettoie les champs facultatifs
            event_campus_combobox.Text = "";
            event_class_combobox.Text = "";
            event_room_combobox.Text = "";
            event_participantname_combobox.Text = "";
            event_participanttype_combobox.Text = "";
            event_period_combobox.Text = "";
            event_subject_combobox.Text = "";

            foreach (events s in (from e in db_remote.events where e.name == events select e))
            {
                event_name_textbox.Text = s.name;
                event_priority_combobox.Text = s.priority.ToString();

                //Affichage de la période
                if (s.period_id != 0)
                {
                    string period_name_temp = (from i in db_remote.periods
                                               where i.id.Equals(s.period_id)
                                               select i.name_period).FirstOrDefault().ToString();

                    string[] period_name = period_name_temp.Split('?');

                    event_period_combobox.Items.Clear();
                    event_period_combobox.Items.Add(period_name[1]);
                    event_period_combobox.SelectedValue = period_name[1];
                }

                //Affichage de la matière
                if (s.subject_id != 0)
                {
                    string subject_name_temp = (from i in db_remote.subjects
                                               where i.id.Equals(s.subject_id)
                                               select i.name).FirstOrDefault().ToString();

                    string[] subject_name = subject_name_temp.Split('_');

                    event_subject_combobox.Items.Clear();
                    event_subject_combobox.Items.Add(subject_name[0]);
                    event_subject_combobox.SelectedValue = subject_name[0];
                }


                //Affichage du nom du participant selon son id et son type

                if (s.type_person != "")
                {

                    string participantName = "";
                    string participantType = "";
                    int participantId = 0;


                    foreach (events event1 in (from e in db_remote.events where e.name == event_name_textbox.Text select e))
                    {
                        participantType = event1.type_person;
                        participantId = Convert.ToInt32(event1.id_person);
                    }

                    switch (participantType)
                    {
                        case "administrators":
                            foreach (administrators s1 in (from e in db_remote.administrators where e.id == participantId select e))
                            { participantName = s1.login; }
                            break;
                        case "managers":
                            foreach (managers s1 in (from e in db_remote.managers where e.id == participantId select e))
                            { participantName = s1.login; }
                            break;
                        case "students":
                            foreach (students s1 in (from e in db_remote.students where e.id == participantId select e))
                            { participantName = s1.login; }
                            break;
                        case "teacher":
                            foreach (teacher s1 in (from e in db_remote.teacher where e.id == participantId select e))
                            { participantName = s1.login; }
                            break;
                        default:
                            participantName = "default";
                            break;

                    }

                    //Affichage du type du participant
                    event_participanttype_combobox.Items.Clear();
                    event_participanttype_combobox.Items.Add("administrators");
                    event_participanttype_combobox.Items.Add("managers");
                    event_participanttype_combobox.Items.Add("teacher");
                    event_participanttype_combobox.Items.Add("students");
                    event_participanttype_combobox.Text = s.type_person;

                    event_participantname_combobox.Items.Clear();
                    chargement_liste_login_event();
                    event_participantname_combobox.SelectedValue = participantName;

                }


                //Affichage du campus
                if (s.campus != "")
                {
                    event_campus_combobox.Items.Clear();
                    chargement_liste_campus_event();
                    event_campus_combobox.SelectedValue = s.campus;
                }

                //Affichage de la classe
                if (s.classe != "")
                {
                    event_class_combobox.Items.Clear();
                    event_class_combobox.Items.Add(s.classe);
                    event_class_combobox.SelectedValue = s.classe;
                }

                //Affichage de la salle attribuée
                if (s.room_id != 0)
                {
                    rooms room_temp = new rooms();

                    string room_name_temp = (from i in db_remote.rooms
                                             where i.id.Equals(s.room_id)
                                             select i.name).FirstOrDefault().ToString();

                    event_room_combobox.Items.Clear();
                    event_room_combobox.Items.Add(room_name_temp);
                    event_room_combobox.SelectedValue = room_name_temp;
                }



                //Séparation du datetime en 2 parties (date et temps)
                string[] startingdate_part = s.T_begin.ToString().Split(' ');
                string[] endingdate_part = s.T_end.ToString().Split(' ');

                //Affichage de la 1e partie du datetime (date de départ)
                event_startingdate_datepicker.Text = startingdate_part[0];

                //Affichage de la 1e partie du datetime (date d'arrivée)
                event_endingdate_datepicker.Text = endingdate_part[0];

                //Affichage de la 2e partie du datetime (heure de départ)
                event_startingdate_combobox.Items.Clear();
                event_startingdate_combobox.Items.Add("00:00:00");
                event_startingdate_combobox.Items.Add("01:00:00");
                event_startingdate_combobox.Items.Add("02:00:00");
                event_startingdate_combobox.Items.Add("03:00:00");
                event_startingdate_combobox.Items.Add("04:00:00");
                event_startingdate_combobox.Items.Add("05:00:00");
                event_startingdate_combobox.Items.Add("06:00:00");
                event_startingdate_combobox.Items.Add("07:00:00");
                event_startingdate_combobox.Items.Add("08:00:00");
                event_startingdate_combobox.Items.Add("09:00:00");
                event_startingdate_combobox.Items.Add("10:00:00");
                event_startingdate_combobox.Items.Add("11:00:00");
                event_startingdate_combobox.Items.Add("12:00:00");
                event_startingdate_combobox.Items.Add("13:00:00");
                event_startingdate_combobox.Items.Add("14:00:00");
                event_startingdate_combobox.Items.Add("15:00:00");
                event_startingdate_combobox.Items.Add("16:00:00");
                event_startingdate_combobox.Items.Add("17:00:00");
                event_startingdate_combobox.Items.Add("18:00:00");
                event_startingdate_combobox.Items.Add("19:00:00");
                event_startingdate_combobox.Items.Add("20:00:00");
                event_startingdate_combobox.Items.Add("21:00:00");
                event_startingdate_combobox.Items.Add("22:00:00");
                event_startingdate_combobox.Items.Add("23:00:00");
                event_startingdate_combobox.SelectedValue = startingdate_part[1];

                //Affichage de la 2e partie du datetime (heure d'arrivée)
                event_endingdate_combobox.Items.Clear();
                event_endingdate_combobox.Items.Add("00:00:00");
                event_endingdate_combobox.Items.Add("01:00:00");
                event_endingdate_combobox.Items.Add("02:00:00");
                event_endingdate_combobox.Items.Add("03:00:00");
                event_endingdate_combobox.Items.Add("04:00:00");
                event_endingdate_combobox.Items.Add("05:00:00");
                event_endingdate_combobox.Items.Add("06:00:00");
                event_endingdate_combobox.Items.Add("07:00:00");
                event_endingdate_combobox.Items.Add("08:00:00");
                event_endingdate_combobox.Items.Add("09:00:00");
                event_endingdate_combobox.Items.Add("10:00:00");
                event_endingdate_combobox.Items.Add("11:00:00");
                event_endingdate_combobox.Items.Add("12:00:00");
                event_endingdate_combobox.Items.Add("13:00:00");
                event_endingdate_combobox.Items.Add("14:00:00");
                event_endingdate_combobox.Items.Add("15:00:00");
                event_endingdate_combobox.Items.Add("16:00:00");
                event_endingdate_combobox.Items.Add("17:00:00");
                event_endingdate_combobox.Items.Add("18:00:00");
                event_endingdate_combobox.Items.Add("19:00:00");
                event_endingdate_combobox.Items.Add("20:00:00");
                event_endingdate_combobox.Items.Add("21:00:00");
                event_endingdate_combobox.Items.Add("22:00:00");
                event_endingdate_combobox.Items.Add("23:00:00");
                event_endingdate_combobox.SelectedValue = endingdate_part[1];



                //Afichage du type
                event_type_combobox.Items.Clear();
                event_type_combobox.Items.Add("Face-to-Face");
                event_type_combobox.Items.Add("E-Learning");
                event_type_combobox.Items.Add("Personal");
                event_type_combobox.SelectedValue = s.type;

                if (s.type != "Personal")
                {
                    //On cache les fonctionnalités réservées aux membres de l'administration (admin + managers)
                    if (usertype != "administrators" && usertype != "managers")
                    {
                        event_more_checkbox.IsEnabled = false;
                        event_updateButton.Visibility = Visibility.Hidden;
                        event_deleteButton.Visibility = Visibility.Hidden;

                    }
                }

                else
                {
                    event_more_checkbox.IsEnabled = true;
                    event_updateButton.Visibility = Visibility.Visible;
                    event_deleteButton.Visibility = Visibility.Visible;
                }



                //Affichage de la priorité
                event_priority_combobox.Items.Clear();
                string priority_item;

                if (s.priority == 1)
                {
                    priority_item = "Mandatory";
                }

                else
                {
                    priority_item = "Optional";
                }

                event_priority_combobox.Items.Add("Optional");
                event_priority_combobox.Items.Add("Mandatory");
                event_priority_combobox.SelectedValue = priority_item;

                if (event_period_combobox.Text != "")
                {
                    calculate_remaining_hours();
                }




            }


        }

        //Rafraichissement des éléments
        private void refresh_events()
        {
            //Obligatoire
            event_name_textbox.Text = "";
            event_priority_combobox.Text = "";
            event_type_combobox.Text = "";
            event_startingdate_datepicker.Text = "";
            event_endingdate_datepicker.Text = "";
            event_startingdate_combobox.Text = "";
            event_endingdate_combobox.Text = "";

            //Facultatif
            event_campus_combobox.Text = "";
            event_class_combobox.Text = "";
            event_room_combobox.Text = "";
            event_participantname_combobox.Text = "";
            event_participanttype_combobox.Text = "";
            event_period_combobox.Text = "";
            event_subject_combobox.Text = "";


            sort_events_by_campus();
        }

        //Listing des périodes attachées au campus/classe
        private void list_periods()
        {
            string current_campus = event_campus_combobox.Text;
            string current_class = event_class_combobox.Text;

            int current_campus_id = 0;
            int current_cursus_id = 0;

            current_campus_id = (from e in db_remote.campus
                                 where e.name == current_campus
                                 select e.id).FirstOrDefault();

            current_cursus_id = (from e in db_remote.classes
                                 where e.name == current_class
                                 select (int)e.cursus_id).FirstOrDefault();


            event_period_combobox.Items.Clear();

            foreach (period_campus period_campus1 in (from e in db_remote.period_campus where e.campus_id == current_campus_id select e))
            {
                try
                {
                    string period1 = (from e in db_remote.periods where e.cursus_id == current_cursus_id && e.id == period_campus1.period_id select e.name_period).FirstOrDefault().ToString();


                    string[] period_name = period1.Split('?');
                    event_period_combobox.Items.Add(period_name[1]);
                }

                catch (Exception)
                {

                }

            }
        }

        private void sort_events_by_campus()
        {

            string current_selected_campus;

            //On affiche les bons événéments selon le tri effectué
            if (event_sort_combobox.Text != "")
            {
                current_selected_campus = event_sort_combobox.SelectedValue.ToString();

                event_listbox.Items.Clear();

                if (current_selected_campus == "All")
                {
                    foreach (events event1 in (from e in db_remote.events
                                               where e.type != "Personal"
                                               select e))
                    {
                        event_listbox.Items.Add(event1.name);
                    }

                    foreach (events event2 in (from e in db_remote.events
                                               where e.type == "Personal" && e.creator_id == userid && e.creator_type == usertype
                                               select e))
                    {
                        event_listbox.Items.Add(event2.name);
                    }

                }

                else if (current_selected_campus == "Personal")
                {
                    foreach (events event1 in (from e in db_remote.events
                                               where e.type == "Personal" && e.creator_id == userid && e.creator_type == usertype
                                               select e))
                    {
                        event_listbox.Items.Add(event1.name);
                    }
                }


                else
                {
                    foreach (events event1 in (from e in db_remote.events
                                               where e.campus == current_selected_campus && e.type != "Personal"
                                               select e))
                    {
                        event_listbox.Items.Add(event1.name);
                    }
                }

            }
       
        }


        private void chargement_liste_campus_event()
        {
            event_campus_combobox.Items.Clear();

            foreach (campus s in (from e in db_remote.campus select e))
            {
                event_campus_combobox.Items.Add(s.name);
            }
        }

        private void chargement_liste_classe_event()
        {
            int id_class_temp = (from i in db_remote.campus
                                 where i.name.Equals(event_campus_combobox.SelectedValue)
                                 select i.id).FirstOrDefault();

            event_class_combobox.Items.Clear();

            foreach (classes s in (from e in db_remote.classes where e.campus_id == id_class_temp select e))
            {
                event_class_combobox.Items.Add(s.name);
            }

        }

        private void chargement_liste_room_event()
        {

            event_room_combobox.Items.Clear();

            foreach (rooms s in (from e in db_remote.rooms where e.campus.Equals(event_campus_combobox.SelectedValue) select e))
            {
                if (s.available == 1)
                {
                    event_room_combobox.Items.Add(s.name);
                }
            }

        }

        private void chargement_liste_login_event()
        {

            string user_type = "";
            user_type = event_participanttype_combobox.SelectedValue.ToString();
            user_type = user_type.Replace("System.Windows.Controls.ComboBoxItem: ", "");

            switch (user_type)
            {
                case "administrators":

                    event_participantname_combobox.Items.Clear();

                    foreach (administrators admin1 in (from e in db_remote.administrators select e))
                    {
                        event_participantname_combobox.Items.Add(admin1.login);
                    }

                    break;

                case "managers":

                    event_participantname_combobox.Items.Clear();

                    foreach (managers manager1 in (from e in db_remote.managers select e))
                    {
                        event_participantname_combobox.Items.Add(manager1.login);
                    }

                    break;

                case "students":

                    event_participantname_combobox.Items.Clear();

                    foreach (students student1 in (from e in db_remote.students select e))
                    {
                        event_participantname_combobox.Items.Add(student1.login);
                    }

                    break;

                case "teacher":

                    event_participantname_combobox.Items.Clear();

                    foreach (teacher teacher1 in (from e in db_remote.teacher select e))
                    {
                        event_participantname_combobox.Items.Add(teacher1.login);
                    }

                    break;

            }

        }


        private void chargement_campus_autorises_event()
        {

            switch (usertype)
            {
                case "administrators":

                    event_sort_combobox.Items.Clear();
                    event_sort_combobox.Items.Add("All");
                    event_sort_combobox.Items.Add("Personal");

                    foreach (campus campus in (from e in db_remote.campus select e))
                    {
                        event_sort_combobox.Items.Add(campus.name);
                        event_sort_combobox.SelectedItem = event_sort_combobox.Items[0];
                    }

                    break;

                case "managers":

                    event_sort_combobox.Items.Clear();
                    event_sort_combobox.Items.Add("Personal");

                    //Récupération, découpage et affichage des campus enregistrés pour un manager

                    string[] table_campus;
                    string registred_campus = usercampus;
                    table_campus = registred_campus.Split('|');

                    for (int i = 0; i < table_campus.Length; i++)
                    {
                        event_sort_combobox.Items.Add(table_campus[i]);
                    }

                    event_sort_combobox.SelectedItem = event_sort_combobox.Items[0];

                    break;

                case "students":

                    event_sort_combobox.Items.Clear();
                    event_sort_combobox.Items.Add("Personal");

                    event_sort_combobox.Items.Add(usercampus);
                    event_sort_combobox.SelectedItem = event_sort_combobox.Items[0];

                    break;

                case "teacher":

                    event_sort_combobox.Items.Clear();
                    event_sort_combobox.Items.Add("Personal");

                    event_sort_combobox.Items.Add(usercampus);
                    event_sort_combobox.SelectedItem = event_sort_combobox.Items[0];

                    break;

            }
        }


        private void list_subjects()
        {
            string current_period = event_period_combobox.SelectedValue.ToString();
            string current_class = event_class_combobox.Text;

            int current_period_id = 0;
            int current_cursus_id = 0;

            current_cursus_id = (from e in db_remote.classes
                                 where e.name == current_class
                                 select (int)e.cursus_id).FirstOrDefault();

            current_period_id = (from e in db_remote.periods
                                 where e.name_period == current_cursus_id + "?" + current_period
                                 select e.id).FirstOrDefault();



            event_subject_combobox.Items.Clear();

            foreach (subjects subject1 in (from e in db_remote.subjects where e.id_period == current_period_id select e))
            {
                try
                {
                    string subject_name_temp = subject1.name;
                    string[] subject_name = subject_name_temp.Split('_');
                    event_subject_combobox.Items.Add(subject_name[0]);
                }

                catch (Exception)
                {

                }

            }
        }

        private void calculate_remaining_hours()
        {
            if (event_subject_combobox.SelectedValue.ToString() != "")
            {

                string current_subject = event_subject_combobox.SelectedValue.ToString();
                string current_period = event_period_combobox.Text;
                string current_class = event_class_combobox.Text;


                int current_cursus_id = (from e in db_remote.classes
                                         where e.name == current_class
                                         select (int)e.cursus_id).FirstOrDefault();

                subjects subject1 = (from i in db_remote.subjects
                                     where i.name.Equals(current_subject + "_s" + current_cursus_id)
                                     select i).FirstOrDefault();

                int current_subject_id = subject1.id;

                int hours_f2f = (int)subject1.nb_hours_face_to_face;
                int hours_elearning = (int)subject1.nb_hours_e_learning;
                int hours_work = (int)subject1.nb_hours_practical_work;
                int hours_eval = (int)subject1.nb_hours_exams;

                int placed_hours_elearning = 0;
                int placed_hours_f2f = 0;
                int placed_hours_work = 0;
                int placed_hours_eval = 0;

                foreach (events event1 in (from e in db_remote.events where e.subject_id == current_subject_id select e))
                {
                    if (event1.type == "E-Learning")
                    {
                        //Séparation du datetime en 2 parties (date et temps)
                        DateTime starting_date = event1.T_begin;
                        DateTime ending_date = event1.T_end;

                        TimeSpan ts = ending_date - starting_date;

                        placed_hours_elearning = placed_hours_elearning + Convert.ToInt32(ts.Hours);

                    }

                    else if (event1.type == "Face-to-Face")
                    {
                        //Séparation du datetime en 2 parties (date et temps)
                        DateTime starting_date = event1.T_begin;
                        DateTime ending_date = event1.T_end;

                        TimeSpan ts = ending_date - starting_date;

                        placed_hours_f2f = placed_hours_f2f + Convert.ToInt32(ts.Hours);

                    }

                    else if (event1.type == "Practical Work")
                    {
                        //Séparation du datetime en 2 parties (date et temps)
                        DateTime starting_date = event1.T_begin;
                        DateTime ending_date = event1.T_end;

                        TimeSpan ts = ending_date - starting_date;

                        placed_hours_work = placed_hours_work + Convert.ToInt32(ts.Hours);

                    }

                    else if (event1.type == "Evaluations")
                    {
                        //Séparation du datetime en 2 parties (date et temps)
                        DateTime starting_date = event1.T_begin;
                        DateTime ending_date = event1.T_end;

                        TimeSpan ts = ending_date - starting_date;

                        placed_hours_eval = placed_hours_eval + Convert.ToInt32(ts.Hours);

                    }


                }

                event_elearning_hours_text.Content = placed_hours_elearning.ToString();
                event_f2f_hours_text.Content = placed_hours_f2f.ToString();
                event_work_hours_text.Content = placed_hours_work.ToString();
                event_eval_hours_text.Content = placed_hours_eval.ToString();

                event_remaining_hours_text.Content = ((hours_f2f + hours_elearning + hours_eval + hours_work) - (placed_hours_elearning + placed_hours_f2f + placed_hours_work + placed_hours_eval)).ToString();

            }


        }


        #endregion
    }
}
